import axios from 'axios'
axios.defaults.baseURL = 'http://ceshi5.dishait.cn'
axios.defaults.timeout = 2000
/* 
 
*/

import store from '@/store/index'
// 请求拦截器
axios.interceptors.request.use((config) => {
    // console.log(config);
    // 添加header头的token
    let token = store.state.user.token
    if (token) {
        config.headers.token = token
    }
    return config;
}, (error) => {
    return error
})

// 添加响应拦截器
export function request(methods, url, data) {
    var p = new Promise((resolve, reject) => {
        if (methods == 'post') {
            axios.post(url, data).then(res => {
                resolve(res)
            }).catch(error => {
                reject(error)
            })
        } else if (methods == 'get') {
            axios.get(url).then(res => {
                resolve (res)
            }).catch(error => {
                reject(error)
            })
        }
    })
    return p
}